package com.wgchao.authmanager.infrastructure.repository;

import com.wgchao.authmanager.domain.model.MenuResource;
import com.wgchao.authmanager.domain.model.Project;
import com.wgchao.authmanager.domain.repository.ProjectRepository;
import com.wgchao.authmanager.infrastructure.exception.IdentifyUsedException;
import com.wgchao.authmanager.infrastructure.jpa.entity.MenuInfo;
import com.wgchao.authmanager.infrastructure.jpa.entity.ProjectInfo;
import com.wgchao.authmanager.infrastructure.jpa.repository.MenuInfoJpaRepository;
import com.wgchao.authmanager.infrastructure.jpa.repository.ProjectInfoJpaRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: ProjectRepositoryImpl
 * @author: wgchao
 * @createTime: 2020/5/9 9:42 AM
 * @Description: TODO
 */
@Component
public class ProjectRepositoryImpl implements ProjectRepository {
    @Autowired
    private ProjectInfoJpaRepository projectInfoJpaRepository;
    @Autowired
    private MenuInfoJpaRepository menuInfoJpaRepository;

    @Override
    public void add(Project project) {
        if(project.findId().compareTo(-1L) == 0){//新增项目
            ProjectInfo projectInfo = projectInfoJpaRepository.findFirstByIdentify(project.getIdentify());
            if(projectInfo != null){
                throw new IdentifyUsedException("项目标识已被使用");
            }
            projectInfo = new ProjectInfo();
            BeanUtils.copyProperties(project, projectInfo);
            projectInfo = projectInfoJpaRepository.save(projectInfo);
        }else{
            ProjectInfo projectInfo = new ProjectInfo();
            BeanUtils.copyProperties(project, projectInfo);
            projectInfo.setId(project.findId());
            projectInfo.setVersion(project.findVersion());
            projectInfo = projectInfoJpaRepository.save(projectInfo);
            //更新菜单资源
            List<String> menuIdentifys = project.getMenuResources().stream().map(menuResource -> {
                return menuResource.getMenuIdentify();
            }).collect(Collectors.toList());
            menuInfoJpaRepository.deleteAllByProjectIdentify(projectInfo.getIdentify());
            menuInfoJpaRepository.findAllByProjectIdentify(projectInfo.getIdentify());//查询一次 清空缓存防止唯一性约束
            if(project.getMenuResources() != null){
                for(MenuResource menuResource : project.getMenuResources()){
                    MenuInfo menuInfo = new MenuInfo();
                    BeanUtils.copyProperties(menuResource, menuInfo);
                    menuInfo.setProjectIdentify(projectInfo.getIdentify());
                    menuInfo = menuInfoJpaRepository.save(menuInfo);
                }
            }
        }
    }

    @Override
    public void remove(Project project, String operator) {
        ProjectInfo projectInfo = projectInfoJpaRepository.findFirstByIdentify(project.getIdentify());
        projectInfo.setDel(true);
        projectInfo = projectInfoJpaRepository.save(projectInfo);
    }

    @Override
    public Project get(String id) {
        ProjectInfo projectInfo = projectInfoJpaRepository.findFirstByIdentifyAndDelIsFalse(id);
        if(projectInfo == null){
            return null;
        }

        List<MenuInfo> menuInfos = menuInfoJpaRepository.findAllByProjectIdentify(id);
        List<MenuResource> menuResources = new ArrayList<>();
        if(menuInfos!=null && !menuInfos.isEmpty()){
            for(MenuInfo menuInfo : menuInfos){
                menuResources.add(new MenuResource(menuInfo.getMenuIdentify(), menuInfo.getName(), menuInfo.getRemark()));
            }
        }
        Project project = new Project(projectInfo.getId(), projectInfo.getIdentify(), projectInfo.getName(),
                projectInfo.getRemark(), projectInfo.isDisabled(), menuResources, projectInfo.getVersion());
        return project;
    }
}
